import {
  reqCancelOrder,
  reqDelOrder,
  reqFollowTarget,
  reqGetpreDetail
} from '@/services/consult'
import { reqGetMedicalOrderDetail } from '@/services/order'
import { reqSendCode } from '@/services/user'
import type { ConsultOrderItem, FollowTargetType } from '@/types/consult'
import type { SendType } from '@/types/user'
import type { OrderDetail } from '@/types/order'
import {
  showFailToast,
  showImagePreview,
  showSuccessToast,
  showToast,
  type FormInstance
} from 'vant'
import { ref } from 'vue'
// 关注和取消关注
export const useFollow = (type: FollowTargetType) => {
  const loading = ref(false)
  // 关注和取消关注
  const follow = async (item: { id: string; likeFlag: 1 | 0 }) => {
    loading.value = true
    try {
      await reqFollowTarget({ type, id: item.id })
      item.likeFlag = item.likeFlag === 1 ? 0 : 1 // 应该做一个子传父的
    } finally {
      loading.value = false
    }
  }
  return { follow, loading }
}

// 查看处方预览
export const useShowPerviewPre = () => {
  const showpreInfo = async (id?: string) => {
    if (id) {
      const res = await reqGetpreDetail(id)
      console.log(res.data.url)
      showImagePreview([res.data.url])
    }
  }
  return { showpreInfo }
}

// 取消问诊
export const useCancelOrder = (cb: () => void) => {
  const loading = ref(false)
  // 取消问诊
  const cancelOrder = async (item: ConsultOrderItem) => {
    try {
      loading.value = true
      // 1. 发送取消的请求
      await reqCancelOrder(item.id)
      loading.value = false
      showSuccessToast('取消成功')
      // 2. 子传父通知父组件更新视图
      // emits('cancelOrder', item.id)
      // console.log('1.直接改状态2.子传父??????')
      cb && cb()
    } catch {
      loading.value = false
      showFailToast('取消失败')
    }
  }
  return { cancelOrder, loading }
}

// 删除订单
export const useDelOrder = (cb: () => void) => {
  const delLoading = ref(false)
  // 删除订单
  const delOrder = async (item: ConsultOrderItem) => {
    try {
      delLoading.value = true
      // 1. 发送取消的请求
      await reqDelOrder(item.id)
      delLoading.value = false
      showSuccessToast('删除成功')
      // 2. 子传父通知父组件更新视图
      // emits('delOrder', item.id)
      // console.log('子传父, 回退')
      cb && cb()
    } catch {
      delLoading.value = false
      showFailToast('删除失败')
    }
  }
  return { delOrder, delLoading }
}
// 获取药品订单详情hook
export const useGetMedicialDetail = (id: string) => {
  const order = ref<OrderDetail>()
  const getMedicalOrderDetail = async () => {
    const res = await reqGetMedicalOrderDetail(id)
    // console.log(res.data)
    order.value = res.data
  }
  getMedicalOrderDetail()
  return { order }
}

// 获取验证码的hook
export const useSendCode = (mobile: Ref<string>, type: SendType) => {
  // 表单组件ref
  const formRef = ref<FormInstance | null>(null)
  // 倒计时的描述
  const time = ref<number>(0)
  // 定时的id
  let timeId: number = -1
  // 发送验证码
  const sendCode = async () => {
    // 1. 禁止到倒计时期间,再次获取验证码
    if (time.value > 0) return
    // 2. 手机号合法性的预校验
    await formRef.value?.validate('mobile')
    time.value = 60
    // 3. 提示一下验证码已发送
    showToast('验证码已发送')
    // 4. 倒计时开始 => 到0之后清除定时器
    clearInterval(timeId)
    timeId = window.setInterval(() => {
      time.value--
      if (time.value === 0) clearInterval(timeId)
    }, 1000)
    // 5. 发送请求
    await reqSendCode(mobile.value, type)
    // 6. 离开组件 清除定时器
    // 7. 视图处理
  }
  // 离开组件 清除定时器
  onUnmounted(() => {
    clearInterval(timeId)
  })
  return { sendCode, formRef, time }
}
